import Vue from 'vue'
import Vuex from 'vuex'
import Cookies from 'js-cookie'
// import conversation from './modules/conversation'
// import group from './modules/group'
// import user from './modules/user'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 用户token
    afToken: '',
    // 用户ID
    afUserID: '',
    // 用户角色
    afUserRole: [],
    // 用户菜单是否需要刷新
    isMenuRef: false,
    // 二级导航页面
    btnRole: [],
    current: Date.now(), // 当前时间
    intervalID: 0
  },
  mutations: {
    setToken (state, userToken) {
      state.afToken = userToken
      Cookies.set('cle', state.afToken)
    },
    setUserID (state, userID) {
      state.afUserID = userID
      Cookies.set('sysUserID', state.afUserID)
    },
    setUserRole (state, role) {
      state.afUserRole = role
      Cookies.set('afUserRole', state.afUserRole)
    },
    startComputeCurrent(state) {
      state.intervalID = setInterval(() => {
        state.current = Date.now()
      }, 500)
    },
    stopComputeCurrent(state) {
      clearInterval(state.intervalID)
      state.intervalID = 0
    }
  },
  actions: {
  },
  // modules: {
  //   conversation,
  //   group,
  //   user
  // },
  getters: {
    getToken (state) {
      let token = state.afToken
      if (!token) {
        token = Cookies.get('cle')
      }
      return token
    },
    hidden(state) {
      // eslint-disable-next-line no-unused-vars
      const temp = state.current 
      if (typeof document.hasFocus !== 'function') {
        return document.hidden
      }
      return !document.hasFocus()
    }
  }
})
